package com.example.prototype.person;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.List;

public class Person implements Cloneable, Serializable {

    private String name;
    private List<String> list;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }


    /**
     * 浅克隆
     *
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Person clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }


    /**
     * 深克隆
     * 使用序列化的方式实现，还可以使用反射生成对象
     *
     * @return
     */
    public Person deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream outputStream = new ObjectOutputStream(bos);
        outputStream.writeObject(this);
        outputStream.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream inputStream = new ObjectInputStream(bis);
        Object o = inputStream.readObject();
        inputStream.close();

        return (Person) o;
    }

    /**
     * json 格式深克隆
     *
     * @return
     */
    public Person jsonClone() {
        Person o = JSON.parseObject(JSON.toJSONString(this), Person.class);
        return o;
    }

}
